package com.qiantu.phone.ui.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.widget.layout.SettingBar;
import com.hjq.widget.view.SwitchButton;
import com.qiantu.api.db.LLRoomDBManager;
import com.qiantu.api.entity.RoomBean;
import com.qiantu.api.entity.SceneDetailBean;
import com.qiantu.phone.R;
import com.qiantu.phone.ui.activity.EditSceneActivityNew;
import com.qiantu.phone.widget.SimpleAnimationDraweeView;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class SceneDetailAdapter extends SceneDetailBaseAdapter {

    public SceneDetailAdapter(EditSceneActivityNew activity, SceneDetailBean mSceneDetailBean){
        super(activity, mSceneDetailBean);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == TOP_VIEW_TYPE) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_edit_scene_top, parent, false);
            return new TopItemViewHolder(view);
        } else if (viewType == DELETE_VIEW_TYPE) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_delete_btn, parent, false);
            return new DeleteItemViewHolder(view);
        } else {
            return super.onCreateViewHolder(parent, viewType);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position, @NonNull List payloads) {
        if (!payloads.isEmpty()) {
            String payload = (String) payloads.get(0);
            switch (payload) {
                case "updateName":
                    updateName((TopItemViewHolder) holder);
                    break;
                case "updateIcon":
                    updateIcon((TopItemViewHolder) holder);
                    break;
                case "updateRoom":
                    updateRoom((TopItemViewHolder) holder);
                    break;
                case "updateInterrupt":
                    updateInterrupt((TopItemViewHolder) holder);
                    break;
//                case "updateBindStatus":
//                    updateBindStatus((TopItemViewHolder) holder);
//                    break;
            }
        }else{
            onBindViewHolder(holder, position);
        }
    }

    private void updateName(TopItemViewHolder topItemViewHolder){
        //更新场景名称
        if(mSceneDetailBean.getName() == null){
            topItemViewHolder.setName.setRightText(R.string.not_set);
        }else{
            topItemViewHolder.setName.setRightText(mSceneDetailBean.getName());
        }
    }

    private void updateIcon(TopItemViewHolder topItemViewHolder){
        //更新场景图片
        String image = mSceneDetailBean.getImage();
        if (TextUtils.isEmpty(image)) {
            topItemViewHolder.sceneIcon.setVisibility(View.GONE);
            topItemViewHolder.selectIcon.setRightText("");
        } else {
            topItemViewHolder.sceneIcon.setVisibility(View.VISIBLE);
//            String suffix = "";
//            switch (ThemeUtil.themeId){
//                case ThemeUtil.THEME_DEFAULT:
//                    suffix = ".webp";
//                    break;
//                case ThemeUtil.THEME_BLACK_GOLD:
//                case ThemeUtil.THEME_ORANGE:
//                    suffix = ".png";
//                    break;
//            }
//            GlideApp.with(mEditSceneActivity)
//                    .load(BuildConfig.APP_SCENE_BASE + ThemeUtil.getIconThemeSuffix() + image + suffix)
//                    //.placeholder(R.drawable.avatar_placeholder_ic)
//                    //.error(R.drawable.avatar_placeholder_ic)
//                    .into(topItemViewHolder.sceneIcon);
            topItemViewHolder.sceneIcon.setImageURI(image);
            topItemViewHolder.selectIcon.getRightView().setText("  ");
        }
    }

    private void updateRoom(TopItemViewHolder topItemViewHolder){
        //更新所属房间
        if(mSceneDetailBean.getLinkType() == 3){
            topItemViewHolder.selectRoom.setRightText(mSceneDetailBean.getFloorName());
        }else{
            if(RoomBean.ROOM_COMMON_SERIALNO.equals(mSceneDetailBean.getRoomSerialNo()) || mSceneDetailBean.getLinkType() == 2){
                topItemViewHolder.selectRoom.setRightText(R.string.common);
            }else{
                topItemViewHolder.selectRoom.setRightText(mSceneDetailBean.getRoomName());
            }
        }
    }

    private void updateFrequently(TopItemViewHolder topItemViewHolder){
        //更新是否常用
        topItemViewHolder.frequentlySwitch.setChecked(mSceneDetailBean.getFavourite());
    }

    private void updateInterrupt(TopItemViewHolder topItemViewHolder){
        //更新场景名称
        if(mSceneDetailBean.getHasActionSetting() != null && mSceneDetailBean.getHasActionSetting()){
            topItemViewHolder.interruptMode.setRightText(R.string.switch_on);
        }else{
            topItemViewHolder.interruptMode.setRightText(R.string.switch_off);
        }
    }

    private class TopItemViewHolder extends RecyclerView.ViewHolder {
        private SettingBar setName;
        private SettingBar selectIcon;
        private SettingBar selectRoom;
        private SwitchButton frequentlySwitch;
//        private View btnCondition;
        private SettingBar interruptMode;
        private ImageView btnSceneConditionSettingMore;
        private SimpleAnimationDraweeView sceneIcon;

        public TopItemViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            setName = itemView.findViewById(R.id.set_name);
            selectIcon = itemView.findViewById(R.id.select_icon);
            sceneIcon = itemView.findViewById(R.id.scene_icon);
            selectRoom = itemView.findViewById(R.id.select_room);
            frequentlySwitch = itemView.findViewById(R.id.frequently_switch);
//            btnCondition = itemView.findViewById(R.id.btn_condition);
            interruptMode = itemView.findViewById(R.id.interrupt_mode);
            btnSceneConditionSettingMore = itemView.findViewById(R.id.btn_scene_condition_setting_more);

            updateName(this);
            updateIcon(this);
            updateRoom(this);
            updateFrequently(this);
            updateInterrupt(this);
//            updateBindStatus(this);
            interruptMode.setVisibility(mSceneDetailBean.getSceneSerialNo() == null ?  View.GONE : View.VISIBLE);

            setName.setOnClickListener(topBottomViewOnClickListener);
            selectIcon.setOnClickListener(topBottomViewOnClickListener);
            sceneIcon.setOnClickListener(topBottomViewOnClickListener);
            selectRoom.setOnClickListener(topBottomViewOnClickListener);
            frequentlySwitch.setOnClickListener(topBottomViewOnClickListener);
//            btnCondition.setOnClickListener(topBottomViewOnClickListener);
            interruptMode.setOnClickListener(topBottomViewOnClickListener);
            btnSceneConditionSettingMore.setOnClickListener(topBottomViewOnClickListener);
        }
    }
}
